package com.renrenyou.duanshipin.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.bumptech.glide.Glide;
import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.base.BaseFragment;
import com.renrenyou.duanshipin.bean.VideoData;
import com.renrenyou.duanshipin.databinding.FragmentMaterialBinding;
import com.renrenyou.duanshipin.material.MaterialActivity;
import com.renrenyou.duanshipin.net.Constance;
import com.renrenyou.duanshipin.net.MyOkhttp;
import com.renrenyou.duanshipin.net.MyOkhttpCallback;
import com.renrenyou.duanshipin.util.NetUtil;
import com.renrenyou.duanshipin.util.ProgressDialogUtil;
import com.renrenyou.duanshipin.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

//素材下载
public class MaterialFragment extends BaseFragment<FragmentMaterialBinding> implements View.OnClickListener {

    private Call mCall;

    @Override
    public FragmentMaterialBinding getBinding(LayoutInflater inflater) {
        return FragmentMaterialBinding.inflate(inflater);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Glide.with(this).load(R.mipmap.ic_material_bg).into(mBinding.ivMaterialBg);
        Glide.with(this).load(R.mipmap.ic_material_apps).into(mBinding.ivMaterialApps);
        mBinding.tvMaterialGet.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (!Constance.isLogin) {
            ToastUtil.showToast(getContext(), "请先登录后再试");
            return;
        }
        Editable editable = mBinding.materialEdt.getText();
        if (editable != null) {
            String content = editable.toString().trim();
            if (TextUtils.isEmpty(content)) {
                ToastUtil.showToast(getContext(), "请先输入内容后再试");
                return;
            }
            loadMaterial(content);
        }
    }

    private void loadMaterial(String shortUrl) {
        if (TextUtils.isEmpty(Constance.TOKEN)) {
            Log.d("materialActivity", "loadMaterial: token is empty!");
            ToastUtil.showToast(getContext(), "登录失效，请重新登录");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("appCode", "com.android.sucai2025");
        params.put("versionNo", "100");
        params.put("channel", "android");
        params.put("shortUrl", shortUrl);

        Map<String, String> header = new HashMap<>();
        header.put("Authorization", Constance.TOKEN);

        AlertDialog progressDialog = ProgressDialogUtil.showProgressDialog(getContext());

        mCall = MyOkhttp.getInstance().post(Constance.BASE_URL + Constance.VIDEO_PARSE, header, params, new MyOkhttpCallback<VideoData>() {
            @Override
            public void onFailed(int errorCode, int errorMsg) {
                progressDialog.dismiss();
                ToastUtil.showToast(getContext(), getString(errorMsg));
            }

            @Override
            public void onSuccess(VideoData videoData) {
                progressDialog.dismiss();
                if (videoData != null) {
                    if (videoData.isSuccess()) {
                        if (videoData.getData() != null) {
                            MaterialActivity.startThisActivity(getContext(),
                                    videoData.getData().getWork_title(),
                                    videoData.getData().getWork_url());
                        }
                    } else {
                        ToastUtil.showToast(getContext(), videoData.getMessage());
                        NetUtil.error(videoData.getCode());
                        if (videoData.getCode() == 401) {
//                            LiveEventBus.get(LogoutEvent.class).post(new LogoutEvent());
                            if (getContext()!=null){
                                Intent intent = new Intent("loginOut");
                                getContext().sendBroadcast(intent);
                            }
                        }
                    }
                } else {
                    ToastUtil.showToast(getContext(), "获取素材失败!");
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("MaterialFragment", "onDestroyView: ");
        if (mCall != null) {
            mCall.cancel();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MaterialFragment", "onDestroy: ");
    }
}
